<HTML>
<HEAD>
<TITLE>[Chapter 2] 2.2 Point</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:24:48 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 2<br>Simple Graphics</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch02_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-2-SECT-2">2.2 Point</A></h2>

<P CLASS=para>
<A NAME="CH02.POINT1"></A>The <tt CLASS=literal>Point</tt> class encapsulates 
x and y coordinates within a single object. It is probably one of the most 
underused classes within Java. Although there are numerous places within 
AWT where you would expect to see a <tt CLASS=literal>Point</tt>, 
its appearances are surprisingly rare. Java 1.1 is starting to 
use <tt CLASS=literal>Point</tt> more heavily. The 
<tt CLASS=literal>Point</tt> class is most often used 
when a method needs to return a pair of coordinates; it lets the method 
return both x and y as a single object. Unfortunately, <tt CLASS=literal>Point</tt> 
usually is not used when a method requires x and y coordinates as arguments; 
for example, you would expect the <tt CLASS=literal>Graphics</tt> 
class to have a version of <tt CLASS=literal>translate()</tt> 
that takes a point as an argument, but there isn't one. 

<P CLASS=para>
The <tt CLASS=literal>Point</tt> class does <I CLASS=emphasis>not</I> represent 
a point on the screen. It is not a visual object; there is no <tt CLASS=literal>drawPoint()</tt> 
method. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-2-SECT-2.1">Point Methods</A></h3>Variables

<P CLASS=para>
The two public variables of <tt CLASS=literal>Point</tt> 
represent a pair of coordinates. They are accessible directly or use the 
<tt CLASS=literal>getLocation()</tt> method. There 
is no predefined origin for the coordinate space. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int x </I><br>
<DD>

<P CLASS=para>
The coordinate that represents the horizontal position. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int y </I><br>
<DD>

<P CLASS=para>
The coordinate that represents the vertical position. </DL>
Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Point () </I><br>
<DD>

<P CLASS=para>
The first constructor creates an instance of <tt CLASS=literal>Point</tt> 
with an initial x value of 0 and an initial y value of 0. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Point (int x, int y) </I><br>
<DD>

<P CLASS=para>
The next constructor creates an instance of <tt CLASS=literal>Point</tt> 
with an initial x value of <tt CLASS=literal>x</tt> 
and an initial y value of <tt CLASS=literal>y</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Point (Point p) </I><br>
<DD>

<P CLASS=para>
The last constructor creates an instance of <tt CLASS=literal>Point</tt> 
from another point, the x value of <tt CLASS=literal>p.x</tt> 
and an initial y value of <tt CLASS=literal>p.y</tt>. </DL>
Locations

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Point getLocation () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getLocation()</tt> method retrieves 
the current location of this point as a new <tt CLASS=literal>Point</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setLocation (int x, int y) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br><I CLASS=emphasis>public void move (int x, int y) <img src="gifs/wstar.gif" alt="(Deprecated)" border=0></I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setLocation()</tt> method changes 
the point's location to (<tt CLASS=literal>x</tt>, 
<tt CLASS=literal>y</tt>). 

<P CLASS=para>
<tt CLASS=literal>move()</tt> is the Java 
1.0 name for this method. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setLocation (Point p) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
This <tt CLASS=literal>setLocation()</tt> method changes 
the point's location to (<tt CLASS=literal>p.x</tt>, 
<tt CLASS=literal>p.y</tt>). 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void translate (int x, int y) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>translate()</tt> method moves 
the point's location 
by adding the parameters (<tt CLASS=literal>x</tt>, 
<tt CLASS=literal>y</tt>) to the corresponding fields 
of the <tt CLASS=literal>Point</tt>. If the original 
<tt CLASS=literal>Point p</tt> is (3, 4) and you call 
<tt CLASS=literal>p.translate(4, -5)</tt>, the new 
value of <tt CLASS=literal>p</tt> is (7, -1). </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int hashCode () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>hashCode()</tt> method returns 
a hash code for the point. The system calls this method when a <tt CLASS=literal>Point</tt> 
is used as the key for a hash table. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean equals (Object object) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>equals()</tt> method overrides 
the <tt CLASS=literal>Object.equals()</tt> 
method to define equality for points. Two <tt CLASS=literal>Point</tt> 
objects are equal if their x and y values are equal. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public String toString () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>Point</tt> 
displays the current values of the x and y variables. For example: 

<DIV CLASS=screen>
<P>
<PRE>
java.awt.Point[x=100,y=200]
</PRE>
</DIV>

</DL>
</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch02_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Graphics</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Dimension</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
